package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.List;

public class Reduce {

    List<Integer> arrayList;

    public Reduce(List<Integer> arrayList) {
        this.arrayList = arrayList;
    }

    public int getMaximum() {
        int max = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i)>max){
                max =arrayList.get(i);
            }
        }
        return max;
    }
    public double getAverage() {
        double avg = 0;
        double sum = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            sum+=arrayList.get(i);
        }
        avg = sum/arrayList.size();
        return avg;
    }
    public double getOrderedMedian() {
        double mid = 0.0;
        if (arrayList.size() % 2 ==0){
            mid =(arrayList.get(arrayList.size()/2-1)+arrayList.get(arrayList.size()/2))/2.0;
        }
        if (arrayList.size() % 2 !=0){
            mid =arrayList.get(arrayList.size()/2);
        }
        return mid;
    }

    public Double getMedianInLinkList(SingleLink singleLink) {

        for (int num : arrayList) {
            singleLink.addTailPointer(num);
        }
        int index = arrayList.size() / 2;
        if (arrayList.size() % 2 == 0) {
            int left = (int) singleLink.getNode(index);
            int right = (int) singleLink.getNode(index + 1);
            return (left + right) / 2.0;
        } else {
            return (Double) singleLink.getNode(index + 1);
        }

    }
    public double getMinimum() {
        int min = arrayList.get(0);
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i)<=min){
                min = arrayList.get(i);
            }
        }
        return min;
    }





    public int getFirstEven() {
        int even = 0;
        for (Integer integer : arrayList) {
            if (integer % 2 ==0){
                even = integer;
                break;
            }
        }
        return even;
    }

    public int getIndexOfFirstEven() {
        int even = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 ==0){
                even = i;
                break;
            }
        }
        return even;
    }
    public int getLastOdd() {
        int lastOdd = 0;
        for (int i = arrayList.size()-1; i >=0 ; i--) {
            if (arrayList.get(i) % 2 !=0){
                lastOdd = arrayList.get(i);
                break;
            }
        }
        return lastOdd;
    }


    public int getIndexOfLastOdd() {
        int lastOdd = 0;
        for (int i = arrayList.size()-1; i >=0 ; i--) {
            if (arrayList.get(i) % 2 !=0){
                lastOdd = i;
                break;
            }
        }
        return lastOdd;
    }

    public boolean isEqual(List<Integer> arrayList) {
        return arrayList.equals(this.arrayList);
    }






}
